import path from "path";
import { ecs } from "../../../core/ecs/ECS";
import { CommonFactory } from "../../../module/common/CommonFactory";
import { GateService } from "../GateService";
import chalk from "chalk";
import { MongoDB } from "../../../module/common/MongoDB";
// import { User } from "../../../module/account/bll/User";

/** 启动网关服务器 */
@ecs.register('GateServerStart')
export class GateServiceStartComp extends ecs.Comp {
    reset(): void { }
}

export class GateServiceStartSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(GateServiceStartComp);
    }

    async entityEnter(e: GateService) {
        let server = CommonFactory.createHsGate();
        e.GateModel.hsGate = server;
        e.GateModel.hcAccount = CommonFactory.createHcAccount();

        e.GateModel.wsHall = CommonFactory.createWssRoom();

        // 如果指定 autoImplementApi 的第 2 个参数为 true，则开启延迟挂载，即延迟到对应接口被调用时才执行挂载操作，加快冷启动速度
        await server.autoImplementApi(path.resolve(__dirname, '../api'), true);
        //server.logger.log(chalk.green(`[网关服务器] 服务已初始化完成`));    

        // 连接数据库
        //await MongoDB.init();
        //server.logger.log(chalk.green(`[网关服务器] 数据库实始化完成`));

        // 启动匹配服务器
        await server.start();
        //server.logger.log(chalk.green(`[网关服务器] 成功启动`));

        // 用户数据表
        //User.init();
    }
}